package com.jackeys.piaems.service.impl;

import com.jackeys.piaems.dao.PaymentDao;
import com.jackeys.piaems.dto.PaymentDetailDto;
import com.jackeys.piaems.entity.Payment;
import com.jackeys.piaems.service.PaymentService;
import com.jackeys.piaems.util.StatusEnumUtil;
import com.jackeys.piaems.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: PaymentServiceImpl
 * @Description: 账单业务层接口实现类
 * @Author: Jackeys
 * @Date: 2021-06-17 20:29
 * @Version: 1.0
 */
@Slf4j
@Service
@Transactional
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentDao paymentDao;

    /**
     * @Name: addPayment
     * @Description: 根据用户id添加账单，实现添加账单的功能
     * @Param: [userId, categoryId, date, cost, remark, flag]
     * @return: com.jackeys.piaems.vo.ResponseVo
     * @Author: Jackeys
     * @Date: 2021/6/17 20:36
     * @Version: 1.0
     */
    @Override
    public ResponseVo addPayment(Long userId, Long categoryId, Float cost, String remark, Integer flag) {
        log.debug("=====================================================================");
        log.debug("添加账单");
        Boolean flag1 = paymentDao.insertPayment(userId, categoryId, new Date(), cost, remark, flag);
        if (flag1) {
            log.debug("添加成功");
            log.debug("=====================================================================");
            return new ResponseVo(StatusEnumUtil.PAYMENT_ADD_SUCCESS);
        }
        log.debug("添加失败");
        log.debug("=====================================================================");
        return new ResponseVo(StatusEnumUtil.PAYMENT_ADD_FAIL);
    }

    /**
     * @Name: deletePayment
     * @Description: 删除账单，实现删除账单的功能
     * @Param: [id]
     * @return: com.jackeys.piaems.vo.ResponseVo
     * @Author: Jackeys
     * @Date: 2021/6/17 20:38
     * @Version: 1.0
     */
    @Override
    public ResponseVo deletePayment(Long id) {
        log.debug("=====================================================================");
        log.debug("删除账单");
        Boolean flag = paymentDao.deletePayment(id);
        if (flag) {
            log.debug("删除成功");
            log.debug("=====================================================================");
            return new ResponseVo(StatusEnumUtil.PAYMENT_DELETE_SUCCESS);
        }
        log.debug("删除失败");
        log.debug("=====================================================================");
        return new ResponseVo(StatusEnumUtil.PAYMENT_DELETE_FAIL);
    }

    /**
     * @Name: updatePayment
     * @Description: 修改账单信息，实现修改账单信息的功能
     * @Param: [id, categoryId, cost, remark, flag]
     * @return: com.jackeys.piaems.vo.ResponseVo
     * @Author: Jackeys
     * @Date: 2021/6/17 20:39
     * @Version: 1.0
     */
    @Override
    public ResponseVo updatePayment(Long id, Long categoryId, Float cost, String remark, Integer flag) {
        log.debug("=====================================================================");
        log.debug("修改账单");
        Boolean flag1 = paymentDao.updatePayment(id, categoryId, cost, remark, flag);
        if (flag1) {
            log.debug("修改成功");
            log.debug("=====================================================================");
            return new ResponseVo(StatusEnumUtil.PAYMENT_UPDATE_SUCCESS);
        }
        log.debug("修改失败");
        log.debug("=====================================================================");
        return new ResponseVo(StatusEnumUtil.PAYMENT_UPDATE_FAIL);
    }

    /**
     * @Name: queryPaymentsByDate
     * @Description: 根据日期来查询用户订单详情，实现查询订单详情的功能
     * @Param: [userId, date]
     * @return: com.jackeys.piaems.vo.ResponseVo
     * @Author: Jackeys
     * @Date: 2021/6/17 20:45
     * @Version: 1.0
     */
    @Override
    public ResponseVo queryPaymentsByDate(Long userId, Date date, Integer flag) {
        log.debug("=====================================================================");
        log.debug("查询账单，日期：" + new SimpleDateFormat("yyyy-MM-dd").format(date));
        List<PaymentDetailDto> paymentDetailDtoList = null;
        try {
            paymentDetailDtoList = paymentDao.selectPaymentsByDate(userId, date, flag);
            if (paymentDetailDtoList == null || paymentDetailDtoList.size() == 0) {
                log.debug("查询失败");
                log.debug("=====================================================================");
                return new ResponseVo(StatusEnumUtil.PAYMENT_QUERY_BY_DATE_FAIL);
            }
        } catch (Exception e) {
            log.debug("查询失败");
            log.debug("=====================================================================");
            return new ResponseVo(StatusEnumUtil.PAYMENT_QUERY_BY_DATE_FAIL);
        }
        log.debug("查询成功");
        log.debug("=====================================================================");
        return new ResponseVo(StatusEnumUtil.PAYMENT_QUERY_BY_DATE_SUCCESS).add("paymentDetailList", paymentDetailDtoList);
    }

    /**
     * @Name: queryTotalInAndOutByUserId
     * @Description: 查询用户总支出与总收入，实现展示总支出与总收入的功能
     * @Param: [userId]
     * @return: com.jackeys.piaems.vo.ResponseVo
     * @Author: Jackeys
     * @Date: 2021/6/26 6:17
     * @Version: 1.0
     */
    @Override
    public ResponseVo queryTotalInAndOutByUserId(Long userId) {
        log.debug("=====================================================================");
        log.debug("查询用户总支出与总收入");
        try {
            Float in = paymentDao.selectInAndOutSum(userId, 1);
            Float out = paymentDao.selectInAndOutSum(userId, 0);
            System.out.println(in + "       " + out);
            log.debug("查询成功");
            log.debug("=====================================================================");
            return new ResponseVo(StatusEnumUtil.PAYMENT_QUERY_IN_AND_OUT_SUCCESS).add("in", in).add("out", out);
        } catch (Exception e) {
            log.debug("查询失败");
            log.debug("=====================================================================");
            e.printStackTrace();
            return new ResponseVo(StatusEnumUtil.PAYMENT_QUERY_IN_AND_OUT_FAIL);
        }

    }

    /**
     * @Name: queryTotalList
     * @Description: 用户查询账单总览, 实现用户查询账单总数
     * @Param: [userId]
     * @return: com.jackeys.piaems.vo.ResponseVo
     * @Author: Jackeys
     * @Date: 2021/6/26 11:28
     * @Version: 1.0
     */
    @Override
    public ResponseVo queryTotalList(Long userId, Integer page, Integer flag) {
        log.debug("=====================================================================");
        log.debug("查询账单，用户：" + userId);
        int pageSize = 12;
        int totalCnt = paymentDao.selectCntByTotal(userId, flag);
        log.debug("查询总数：" + totalCnt);
        int start = (page - 1) * pageSize;
        List<Payment> paymentList = paymentDao.selectTotalPaymentList(userId, start, pageSize, flag);
        if (paymentList == null || paymentList.size() == 0) {
            log.debug("查询失败");
            log.debug("=====================================================================");
            return new ResponseVo(StatusEnumUtil.PAYMENT_QUERY_TOTAL_FAIL);
        }
        log.debug("查询成功");
        log.debug("=====================================================================");
        return new ResponseVo(StatusEnumUtil.PAYMENT_QUERY_TOTAL_SUCCESS).add("paymentList", paymentList).add("totalCnt", totalCnt);

    }

    /**
     * @Name: selectAllPayment
     * @Description: 返回用户所有数据
     * @Param: []
     * @return: com.jackeys.piaems.vo.ResponseVo
     * @Author: Jackeys
     * @Date: 2021/6/28 22:35
     * @Version: 1.0
     */
    @Override
    public ResponseVo selectAllPayment(Long userId, Integer flag) {
        log.debug("=====================================================================");
        log.debug("查询所有账单");
        List<Payment> paymentList=paymentDao.selectAllPayment(userId,flag);
        if(paymentList==null||paymentList.size()==0){
            log.debug("查询失败");
            log.debug("=====================================================================");
            return new ResponseVo(StatusEnumUtil.PAYMENT_QUERY_ALL_FAIL);
        }
        log.debug("查询成功");
        log.debug("=====================================================================");
        return new ResponseVo(StatusEnumUtil.PAYMENT_QUERY_ALL_SUCCESS).add("paymentList",paymentList);
    }
}
